package com.wswy.wzcx.ui.component;

import android.net.Uri;
import android.text.TextUtils;
import android.view.ViewGroup;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.listener.RequestLoggingListener;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.ui.data.UserMessage;
import com.wswy.wzcx.ui.other.AndroidUtilities;

/**
 * Created by wangduheng26 on 2018/3/20.
 * wangduheng26@gmail.com
 */

public class UserMessageComponent2 extends BaseFeedbackComponent {

    public UserMessageComponent2(ViewGroup parent) {
        super(parent, R.layout.item_message_user);
        ivHeader = getView().findViewById(R.id.image_view_header);
        tvContent = getView().findViewById(R.id.tv_content);
        imageContent = getView().findViewById(R.id.img_content);
        tvTime = getView().findViewById(R.id.tv_time);
        if (DataCenter.get().hasLogin()) {
            userMode = DataCenter.get().getUserMode();
        }
    }


    @Override
    public void bind(UserMessage.Message data) {
        super.bind(data);
        if(userMode != null) {
            Uri uri = UriUtil.parseUriOrNull(userMode.getHeadImg());
            ImageRequest builder = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(ResizeOptions.forSquareSize(AndroidUtilities.dp(100)))
                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
                .setRequestListener(new RequestLoggingListener())
                .build();
            AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
                .setOldController(ivHeader.getController())
                .setImageRequest(builder)
                .build();
            ivHeader.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);
            ivHeader.setController(controller);
        }


        if(!TextUtils.isEmpty(data.image)){
            displayImage(data.image,true);
        }

    }

}
